package lab03.test07;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

//7、	读取book.zip,并将book.zip中含有的文件重新存放到当前目录中的book文件夹中，
// 即将book.zip的内容解压到book文件夹中。
public class Test {

    public static void main(String[] args) {


        String path = "D:\\test.zip";

        try {
            ZipFile zipFile = new ZipFile(path);

            Enumeration entries = zipFile.entries();

            ZipEntry zipEntry = null;

            while (entries.hasMoreElements()) {

                zipEntry = (ZipEntry) entries.nextElement();
                System.out.println(zipEntry.getName());
                //创建test文件夹
                File testFile = new File("D:\\test");
                testFile.mkdir();
                //  File.separator();对Windows和UNIX进行判断分隔符\\和/

                //通过字节流 读文件 写文件
                InputStream fileInputStream = zipFile.getInputStream(zipEntry);
                FileOutputStream fileOutputStream = new FileOutputStream(
                        new File("D:\\test\\" + zipEntry.getName()));


                byte[] data = new byte[1024];
                while ((fileInputStream.read(data)) != -1) {

                    fileOutputStream.write(data);
                }

                fileInputStream.close();
                fileOutputStream.close();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

